# Shallow and deep copy

x = [1, 2, 3]
y = x
print(x) # [1, 2, 3]
print(y) # [1, 2, 3]
x[1] = 100
print(x) # [1, 100, 3]
print(y) # [1, 100, 3]

x = [1, 2, 3]
# 浅拷贝1
y = x.copy()
print(x) # [1, 2, 3]
print(y) # [1, 2, 3]
x[1] = 100
print(x) # [1, 100, 3]
print(y) # [1, 2, 3]

x = [1, 2, 3]
# 浅拷贝2
y = x[ : ] # 切整个 x 列表
print(x) # [1, 2, 3]
print(y) # [1, 2, 3]
x[1] = 100
print(x) # [1, 100, 3]
print(y) # [1, 2, 3]

# 深拷贝

# 引入问题
x = [[1,2,3],[4,5,6],[7,8,9]]
y = x.copy()
# 修改原始列表中间元素的值
x[1][1] = 100
print(x) # [[1, 2, 3], [4, 100, 6], [7, 8, 9]]
print(y) # [[1, 2, 3], [4, 100, 6], [7, 8, 9]] 发现问题：此时 y 的中间元素也被修改了，看来在嵌套列表中，浅拷贝的方式是不行的

# 要解决该问题，要使用深拷贝
import copy

x = [[1,2,3],[4,5,6],[7,8,9]]
# 使用 copy 模块的浅拷贝方法（不能解决上面的问题）
y = copy.copy(x)
# 修改原始列表中间元素的值
x[1][1] = 100
print(x) # [[1, 2, 3], [4, 100, 6], [7, 8, 9]]
print(y) # [[1, 2, 3], [4, 100, 6], [7, 8, 9]] 

# 深拷贝
x = [[1,2,3],[4,5,6],[7,8,9]]
# 使用 copy 模块的深拷贝方法（不能解决上面的问题）
y = copy.deepcopy(x)
# 修改原始列表中间元素的值
x[1][1] = 100
print(x) # [[1, 2, 3], [4, 100, 6], [7, 8, 9]]
print(y) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # 此时，拷贝出来的列表不会改变了
 


